//
//  GameController.cpp
//  CandyDizzle
//
//  Created by Cloner Team on 8/2/14.
//
//

#include "GameController.h"
GameController::GameController(){
    this->gameModel = GameModel::getInstance();
    this->director = Director::getInstance();
    this->soundModel = SoundModel::getInstance();
    this->registerHandler(1,this,ug_handler_func(GameController::processEvent));
    this->registerHandlerByEventType(EVT_CLICK_BUTTON, 0, this, ug_handler_func(GameController::processButtonSoundEffect));
    
}

GameController::~GameController(){
    Utils::safeRelease(this->director);
    Utils::safeRelease(this->gameModel);
    Utils::safeRelease(this->soundModel);
}

void GameController::processButtonSoundEffect(UGEvent* event){
    this->soundModel->playEffect(EF_BUTTON,false,0.0f);
}

void GameController::processEvent(UGEvent* event){
    if (event->getId() == REQ_BEGIN_GAME) {
        Scene* gameScene = GameScene::create();
        this->director->runWithScene(gameScene);
        this->gameModel->fireDisplayMenu();
        this->soundModel->playBackgroundMusic(BG_MUSIC_MENU, true, 0.0f, 0.0f);
        return;
        
    }
    
    if (event->getId() == REQ_PLAY_GAME) {
        this->gameModel->setNumberGames(this->gameModel->getNumberGames()+1);
        this->gameModel->saveInformation();
        this->soundModel->playBackgroundMusic(BG_MUSIC_PLAY, true, 0.0f, 0.0f);
        
        this->gameModel->setGameState(GS_PLAY);
        IncreaseSpeed::multiply = 1;
        this->gameModel->setCurrentScore(0);
        this->gameModel->fireDisplayGame();
    }
    
    if (event->getId() == REQ_INCREASE_SPEED) {
        this->soundModel->playEffect(EF_INCREASE_SPEED, false, 0.0f);
        this->gameModel->fireIncreaseSpeed();
        this->gameModel->hasIncreasedSpeed = true;
    }
    
    if (event->getId() == REQ_CORRECT_CHOICE) {
        this->soundModel->playEffect(EF_CORRECT, false, 0.0f);
        this->gameModel->handleCorrectChoice();
        this->gameModel->setCurrentScore(gameModel->getCurrentScore()+1);
    }
    
    if (event->getId() == REQ_FINISH_GAME) {
        if(this->gameModel->getHighScore() < this->gameModel->getCurrentScore()) {
            this->soundModel->playEffect(EF_HIGHSOCRE, false, 0.0f);
            this->gameModel->setHighScore(this->gameModel->getCurrentScore());
            this->gameModel->saveInformation();
        } else {
            this->soundModel->playEffect(EF_NO_HIGHSCORE, false, 0.0f);
        }
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        AppUtils::reportScore(this->gameModel->getHighScore(), (char*)"colourassault");
#endif
        this->soundModel->playBackgroundMusic(BG_MUSIC_MENU, true, 0.0f, 0.0f);
        this->gameModel->setGameState(GS_MENU);
        this->gameModel->finishGame();
        this->gameModel->fireDisplayResult();
    }
}
